<?php
/* 	These code was generated using phpCIGen v 0.1.b (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	
	+ Module  		: t_bill Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_t_bill.php
 	+ creator 		: 
 	+ Created on 06/Mar/2012 12:35:49
	
*/

//class of t_bill
class C_t_bill extends CI_Controller {

	//constructor
	function C_t_bill(){
		parent::__construct();
		session_start();
		$this->load->model('m_t_bill', '', TRUE);
	}
	
	//set index
	function index(){
		$this->load->view('main/v_t_bill');
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->t_bill_list();
				break;
			case "UPDATE":
				$this->t_bill_update();
				break;
			case "CREATE":
				$this->t_bill_create();
				break;
			case "DELETE":
				$this->t_bill_delete();
				break;
			case "SEARCH":
				$this->t_bill_search();
				break;
			case "PRINT":
				$this->t_bill_print();
				break;
			case "EXCEL":
				$this->t_bill_export_excel();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	//function fot list record
	function t_bill_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_t_bill->t_bill_list($query,$start,$end);
		echo $result;
	}
	
	//function for create new record
	function t_bill_create(){
		//POST varible here
		//auto increment, don't accept anything from form values
		$t_order_id=@$_POST["t_order_id"];
		$t_order_id=is_numeric($t_order_id)?$t_order_id:0;
		$bill_date=trim(@$_POST["bill_date"]);
		$bill_no=trim(@$_POST["bill_no"]);
		$bill_no=htmlentities($bill_no,ENT_QUOTES);
		$bill_value=trim(@$_POST["bill_value"]);
		$bill_note=trim(@$_POST["bill_note"]);
		$bill_note=htmlentities($bill_note,ENT_QUOTES);
		$bill_stat=trim(@$_POST["bill_stat"]);
		$bill_stat=htmlentities($bill_stat,ENT_QUOTES);
		$result=$this->m_t_bill->t_bill_create($t_order_id ,$bill_date ,$bill_no ,$bill_value ,$bill_note ,$bill_stat );
		echo $result;
	}
	
	
	//function for update record
	function t_bill_update(){
		//POST variable here
		$bill_id=@$_POST["bill_id"];
		$bill_id=is_numeric($bill_id)?$bill_id:0;
		$t_order_id=@$_POST["t_order_id"];
		$t_order_id=is_numeric($t_order_id)?$t_order_id:0;
		$bill_date=trim(@$_POST["bill_date"]);
		$bill_no=trim(@$_POST["bill_no"]);
		$bill_no=htmlentities($bill_no,ENT_QUOTES);
		$bill_value=trim(@$_POST["bill_value"]);
		$bill_note=trim(@$_POST["bill_note"]);
		$bill_note=htmlentities($bill_note,ENT_QUOTES);
		$bill_stat=trim(@$_POST["bill_stat"]);
		$bill_stat=htmlentities($bill_stat,ENT_QUOTES);
		$result = $this->m_t_bill->t_bill_update($bill_id,$t_order_id,$bill_date,$bill_no,$bill_value,$bill_note,$bill_stat);
		echo $result;
	}
	
	//function for delete selected record
	function t_bill_delete(){
		$ids = @$_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_t_bill->t_bill_delete($pkid);
		echo $result;
	}

	//function for advanced search
	function t_bill_search(){
		//POST varibale here
		$bill_id=@$_POST["bill_id"];
		$bill_id=is_numeric($bill_id)?$bill_id:0;
		$t_order_id=@$_POST["t_order_id"];
		$t_order_id=is_numeric($t_order_id)?$t_order_id:0;
		$bill_date=trim(@$_POST["bill_date"]);
		$bill_no=trim(@$_POST["bill_no"]);
		$bill_no=htmlentities($bill_no,ENT_QUOTES);
		$bill_value=trim(@$_POST["bill_value"]);
		$bill_note=trim(@$_POST["bill_note"]);
		$bill_note=htmlentities($bill_note,ENT_QUOTES);
		$bill_stat=trim(@$_POST["bill_stat"]);
		$bill_stat=htmlentities($bill_stat,ENT_QUOTES);
		
		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$result = $this->m_t_bill->t_bill_search($bill_id ,$t_order_id ,$bill_date ,$bill_no ,$bill_value ,$bill_note ,$bill_stat ,$start,$end);
		echo $result;
	}


	function t_bill_print(){
  		//POST varibale here
		$bill_id=@$_POST["bill_id"];
		$bill_id=is_numeric($bill_id)?$bill_id:0;
		$t_order_id=@$_POST["t_order_id"];
		$t_order_id=is_numeric($t_order_id)?$t_order_id:0;
		$bill_date=trim(@$_POST["bill_date"]);
		$bill_no=trim(@$_POST["bill_no"]);
		$bill_no=htmlentities($bill_no,ENT_QUOTES);
		$bill_value=trim(@$_POST["bill_value"]);
		$bill_note=trim(@$_POST["bill_note"]);
		$bill_note=htmlentities($bill_note,ENT_QUOTES);
		$bill_stat=trim(@$_POST["bill_stat"]);
		$bill_stat=htmlentities($bill_stat,ENT_QUOTES);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$data["data_print"] = $this->m_t_bill->t_bill_print($bill_id ,$t_order_id ,$bill_date ,$bill_no ,$bill_value ,$bill_note ,$bill_stat ,$option,$filter);
		$print_view=$this->load->view("main/p_t_bill.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/t_bill_printlist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';        
	}
	/* End Of Function */

	/* Function to Export Excel document */
	function t_bill_export_excel(){
		//POST varibale here
		$bill_id=@$_POST["bill_id"];
		$bill_id=is_numeric($bill_id)?$bill_id:0;
		$t_order_id=@$_POST["t_order_id"];
		$t_order_id=is_numeric($t_order_id)?$t_order_id:0;
		$bill_date=trim(@$_POST["bill_date"]);
		$bill_no=trim(@$_POST["bill_no"]);
		$bill_no=htmlentities($bill_no,ENT_QUOTES);
		$bill_value=trim(@$_POST["bill_value"]);
		$bill_note=trim(@$_POST["bill_note"]);
		$bill_note=htmlentities($bill_note,ENT_QUOTES);
		$bill_stat=trim(@$_POST["bill_stat"]);
		$bill_stat=htmlentities($bill_stat,ENT_QUOTES);
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$query = $this->m_t_bill->t_bill_export_excel($bill_id ,$t_order_id ,$bill_date ,$bill_no ,$bill_value ,$bill_note ,$bill_stat ,$option,$filter);
		
		to_excel($query,"t_bill"); 
		echo '1';
			
	}
	
	function cetak(){
		//POST varibale here
		$bill_id = trim(@$_POST["bill_id"]);
		$jenis = trim(@$_POST["jenis"]);
	
		$result = $this->m_t_bill->cetak($bill_id);
		$rs=$result->row();
		$detail_bill=$result->result();
		$nbrows = $result->num_rows();
		
		if($nbrows>0){
			$data['bill_no']=$rs->bill_no;
			$data['customer_name']=$rs->customer_name;
			$data['customer_address']=$rs->customer_address;
			$data['bill_value']=$rs->bill_value;
			$data['order_note']=$rs->order_note;
			$data['bill_date']=date('d-m-Y', strtotime($rs->bill_date));
			$total = 0;
			foreach($detail_bill as $list => $row) {
				$total += $row->jumlah;
			}
			$data['terbilang'] = $this->terbilang($total, 1);
			$data['data_print']=$detail_bill;
			
			if($jenis=='kuitansi'){
				$viewdata=$this->load->view("main/bill_formcetak_kuitansi",$data,TRUE);
				$file = fopen("bill_kuitansi_paper.html",'w');
			}elseif ($jenis=='nota'){
				$viewdata=$this->load->view("main/bill_formcetak_nota",$data,TRUE);
				$file = fopen("bill_nota_paper.html",'w');
			}elseif ($jenis=='srtjln'){
				$viewdata=$this->load->view("main/bill_formcetak_srtjln",$data,TRUE);
				$file = fopen("bill_srtjln_paper.html",'w');
			}
			fwrite($file, $viewdata);
			fclose($file);
		}
		if($nbrows>0){
			echo '1';
		}else{
			echo '0';
		}
	}
	
	#
	function kekata($x) {
		#
		$x = abs($x);
		#
	$angka = array("", "satu", "dua", "tiga", "empat", "lima",
		#
	    "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
		#
	$temp = "";
		#
	if ($x <12) {
			#
		$temp = " ". $angka[$x];
			#
	} else if ($x <20) {
		#
		$temp = $this->kekata($x - 10). " belas";
		#
	} else if ($x <100) {
		#
		$temp = $this->kekata($x/10)." puluh". $this->kekata($x % 10);
		#
	} else if ($x <200) {
		#
		$temp = " seratus" . $this->kekata($x - 100);
		#
	} else if ($x <1000) {
		#
		$temp = $this->kekata($x/100) . " ratus" . $this->kekata($x % 100);
		#
	} else if ($x <2000) {
		#
		$temp = " seribu" . $this->kekata($x - 1000);
		#
	} else if ($x <1000000) {
		#
		$temp = $this->kekata($x/1000) . " ribu" . $this->kekata($x % 1000);
		#
	} else if ($x <1000000000) {
		#
		$temp = $this->kekata($x/1000000) . " juta" . $this->kekata($x % 1000000);
		#
	} else if ($x <1000000000000) {
		#
		$temp = $this->kekata($x/1000000000) . " milyar" . $this->kekata(fmod($x,1000000000));
		#
	} else if ($x <1000000000000000) {
		#
		$temp = $this->kekata($x/1000000000000) . " trilyun" . $this->kekata(fmod($x,1000000000000));
		#
	}
	#
	return $temp;
	#
	}
	
	
	function terbilang($x, $style=4) {
		#
			if($x<0) {
			#
			$hasil = "minus ". trim($this->kekata($x));
		#
		} else {
			#
			$hasil = trim($this->kekata($x));
		#
		}
		#
		switch ($style) {
			#
			case 1:
				#
			$hasil = strtoupper($hasil);
		#
		break;
		#
			case 2:
			#
			$hasil = strtolower($hasil);
		#
		break;
		#
			case 3:
			#
			$hasil = ucwords($hasil);
		#
		break;
		#
			default:
			#
			$hasil = ucfirst($hasil);
		#
		break;
		#
		}
		#
		return $hasil;
		#
	}
	
	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}
	
	
}
?>